//
//  ScenicDetailVC.m
//  ImGuider
//
//  Created by llt on 2017/8/29.
//  Copyright © 2017年 AudioCtrip. All rights reserved.
//

#import "ScenicDetailVC.h"
#import "ScenicDetailIntroduce.h"
#import "TitleContentCell.h"
#import "ScenicRecommendTicketCell.h"
#import "SectionTitleHeaderView.h"
#import "TickectDetailVC.h"
#import "MerchantInforCell.h"
#import "RecommendMerchantCell.h"
#import "RecommendMerchantDetailVC.h"

@interface ScenicDetailVC ()<UITableViewDelegate,UITableViewDataSource,ScenicDetailIntroduceDelegate,ScenicRecommendTicketCellDelegate>

@property (weak, nonatomic) IBOutlet UITableView *tableView;

@property (nonatomic, strong)NSMutableArray * tagArray;

@end

@implementation ScenicDetailVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    self.barBGAlpha = 0;
    
    self.subScrollView = self.tableView;
    
    
    [self setup];
    // [self configInfo];
}

- (void)setup {
    
    [self setAutoRowHeight:self.tableView];
    
    [self.tableView registerNib:[UINib nibWithNibName:@"ScenicDetailIntroduce" bundle:nil] forCellReuseIdentifier:@"ScenicDetailIntroduce"];
    [self.tableView registerNib:[UINib nibWithNibName:@"TitleContentCell" bundle:nil] forCellReuseIdentifier:@"TitleContentCell"];
    
    [self.tableView registerNib:[UINib nibWithNibName:@"ScenicRecommendTicketCell" bundle:nil] forCellReuseIdentifier:@"ScenicRecommendTicketCell"];
    [self.tableView registerClass:[SectionTitleHeaderView class] forHeaderFooterViewReuseIdentifier:@"SectionTitleHeaderView"];
    
    [self.tableView registerNib:[UINib nibWithNibName:@"MerchantInforCell" bundle:nil] forCellReuseIdentifier:@"MerchantInforCell"];
    
    [self.tableView registerNib:[UINib nibWithNibName:@"RecommendMerchantCell" bundle:nil] forCellReuseIdentifier:@"RecommendMerchantCell"];
    
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    self.tagArray =[NSMutableArray arrayWithArray:@[@(MerchantTypeName),@(MerchantTypeTel),@(MerchantTypeWeb)]];
}

- (void)setTagArray:(NSMutableArray *)tagArray {
    
    _tagArray = [NSMutableArray arrayWithArray:tagArray];
    
        if (!(self.scenic.viewname.length > 0)) {
        
           [_tagArray removeObject:@(MerchantTypeName)];
        }
    
        if (!(self.scenic.telephone.length > 0)) {
          
            [_tagArray removeObject:@(MerchantTypeTel)];
        }
    
        if (!(self.scenic.website.length > 0)) {
           [_tagArray removeObject:@(MerchantTypeWeb)];
        }
}

#pragma mark  --ScenicDetailIntroduceDelegate

- (void)goAction:(NSInteger)type {
    
    switch (type) {
        case 1:
            [self phoneAction];
            break;
        case 2:
            [self websiteAction];
            break;
        case 3:
            [self navAction];
            break;
        default:
            break;
    }
}


#pragma mark - UIScrollViewDelegate

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    
    CGFloat offsetY = scrollView.contentOffset.y;
    if (!self.canScroll) {
        [scrollView setContentOffset:CGPointZero];
    }
    
    if (offsetY < 0 ) {
        
        [[NSNotificationCenter defaultCenter] postNotificationName:@"kLeaveTopNtf" object:@1];
        self.canScroll = NO;
        scrollView.contentOffset = CGPointZero;
    }
}


- (void)navAction{
    
    CLLocationCoordinate2D coordinate = CLLocationCoordinate2DMake(self.scenic.lat.doubleValue, self.scenic.lng.doubleValue);
    [IGUtil tractficToLocation:transformFromWGSToGCJ(coordinate, self.scenic.country) vc:self];
}
- (void)phoneAction{
    NSString * phoneNum = [[self.scenic.telephone stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] stringByReplacingOccurrencesOfString:@" " withString:@""];
    NSString * str = [NSString stringWithFormat:@"tel://%@",phoneNum];
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:str]];
}
- (void)websiteAction{
    
    NSString * str = [self.scenic.website stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
    NSURL *url= [NSURL URLWithString:str];
    [[UIApplication sharedApplication] openURL:url];
}


- (void)reserveTicketWith:(NSString *)ticketID {
    
    TickectDetailVC * vc = [[TickectDetailVC alloc] init];
    
    vc.ticketID = ticketID;
    
    [self.superVC.navigationController pushViewController:vc animated:YES];
    
}

#pragma mark    --UITableViewDelegate,UITableViewDataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return self.scenic.business.count > 0 ? 4 : 3;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    switch (section) {
        case 0:
        {
            return self.tagArray.count;
        }
            break;
        case 1:
        {
            if (self.scenic.ticketlist.count) {
                
                return self.scenic.ticketlist.count;
            }
            
            if (self.scenic.ticket.length > 0) {
                
                return 1;
            }
        }
            break;
        case 2:
        {
            NSInteger num = 0;
            
            if (self.scenic.openinfo.length > 0) {
                
                num += 1;
            }
            if (self.scenic.address.length > 0) {
                num += 1;
            }
            
            return num;
        }
            break;
        case 3:
        {
            return self.scenic.business.count;
        }
            break;
        default:
            break;
    }
    
    return 0;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    switch (indexPath.section) {
        case 0:
        {
            NSNumber * type = self.tagArray[indexPath.row];
            
            if (type.integerValue == MerchantTypeName) {
                
                ScenicDetailIntroduce * cell = [tableView dequeueReusableCellWithIdentifier:@"ScenicDetailIntroduce"];
                cell.delegate = self;
                [cell configData:self.scenic];
                
                return cell;
            }else {
                
                MerchantInforCell * cell = [tableView dequeueReusableCellWithIdentifier:@"MerchantInforCell"];
                cell.type = type.integerValue;
                cell.delegate = self;
                cell.scenic =self.scenic;
                
                return cell;
            }
            
        }
            break;
        case 1:
        {
            
            if ([AppManager sharedManager].hasTicket) {
                
                if (self.scenic.ticketlist.count) {
                    
                    ScenicRecommendTicketCell * cell = [self.tableView dequeueReusableCellWithIdentifier:@"ScenicRecommendTicketCell"];
                    
                    TicketDetailModel * model = self.scenic.ticketlist[indexPath.row];
                    
                    cell.delegate = self;
                    [cell configDataWith:model];
                    return cell;
                }
            }
            
            if (self.scenic.ticket.length > 0) {
                
                TitleContentCell * cell = [tableView dequeueReusableCellWithIdentifier:@"TitleContentCell"];
                
                [cell configDataWith:MYLocalizedString(@"SITELIST_TICKET", nil) Content:self.scenic.ticket];
                
                return cell;
            }
        }
            break;
        case 2:
        {
            TitleContentCell * cell = [tableView dequeueReusableCellWithIdentifier:@"TitleContentCell"];
            
            if (self.scenic.openinfo.length > 0 && self.scenic.address.length > 0) {
                
                if (indexPath.row == 0) {
                    
                    [cell configDataWith:MYLocalizedString(@"SITELIST_OPENINGHOURS", nil) Content:self.scenic.openinfo];
                }else{
                    [cell configDataWith:MYLocalizedString(@"SITELIST_ADDRESS", nil) Content:self.scenic.address];
                    
                }
            }else if(self.scenic.openinfo.length > 0){
                [cell configDataWith:MYLocalizedString(@"SITELIST_OPENINGHOURS", nil) Content:self.scenic.openinfo];
            }else if(self.scenic.address.length > 0){
                [cell configDataWith:MYLocalizedString(@"SITELIST_ADDRESS", nil) Content:self.scenic.address];
            }
            
            return cell;
        }
            break;
        case 3:
        {
            
            MerchantModel *model = self.scenic.business[indexPath.row];
            RecommendMerchantCell * cell = [tableView dequeueReusableCellWithIdentifier:@"RecommendMerchantCell"];
            cell.model = model;
            cell.superVC = self.superVC;
            return cell;
        }
            break;
        default:
            break;
    }
    
    return nil;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    
    if ([AppManager sharedManager].hasTicket) {
        
        if (section == 1 && self.scenic.ticketlist.count > 0) {
            SectionTitleHeaderView * headerView = [self.tableView dequeueReusableHeaderFooterViewWithIdentifier:@"SectionTitleHeaderView"];
            headerView.contentView.backgroundColor = [UIColor whiteColor];
            headerView.titleLabel.text = MYLocalizedString(@"SITELIST_TICKET", nil);
            headerView.titleLabel.font = Font(14);
            headerView.titleLabel.textColor = kSubitleColor;
            return headerView;
        }
    }
    if(section == 3) {
        
        SectionTitleHeaderView * headerView = [self.tableView dequeueReusableHeaderFooterViewWithIdentifier:@"SectionTitleHeaderView"];
        headerView.contentView.backgroundColor = [UIColor whiteColor];
        headerView.titleLabel.text = [NSString stringWithFormat:@"%@%@",MYLocalizedString(@"DESTINATION_NEARBY", nil),MYLocalizedString(@"DESTINATION_RECOMMED", nil)] ;
        headerView.titleLabel.font = Font(14);
        headerView.titleLabel.textColor = kSubitleColor;
        return headerView;
    }
    
    return nil;
}


- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    
    
    return nil;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    
    if (section == 0) {
        
        return 10.f;
    }

    if ([AppManager sharedManager].hasTicket) {
        
        if (section == 1 && (self.scenic.ticketlist.count > 0)){
            
            return 40.f;
        }
    }
    if(section == 3){
         return 40.f;
    }
    
    return 0.00001;
}


- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    
    return 10.f;
}

@end

